{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### Pillow方式",
   "id": "63e08fcd03af671f"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-30T12:40:21.254673Z",
     "start_time": "2025-04-30T12:40:20.952096Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "from PIL import Image\n",
    "im = Image.open('files/test.jpg')\n",
    "im.size"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3456, 2234)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T12:40:21.425707Z",
     "start_time": "2025-04-30T12:40:21.308194Z"
    }
   },
   "cell_type": "code",
   "source": [
    "im_array = np.array(im)\n",
    "im_array.shape # 第三个维度是色阶"
   ],
   "id": "fa879a7b4d77156",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2234, 3456, 3)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T17:16:48.142235Z",
     "start_time": "2025-04-30T17:16:48.130235Z"
    }
   },
   "cell_type": "code",
   "source": [
    "im_pillow_c1 = im_array[:, :, 0]\n",
    "im_pillow_c2 = im_array[:, :, 1]\n",
    "im_pillow_c3 = im_array[:, :, 2]"
   ],
   "id": "17c00922f6ea737e",
   "outputs": [],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T17:16:50.661632Z",
     "start_time": "2025-04-30T17:16:50.650631Z"
    }
   },
   "cell_type": "code",
   "source": [
    "im_pillow_c1 = im_pillow_c1[:,:, np.newaxis] #拼接维度\n",
    "im_pillow_c2 = im_pillow_c2[:, :, np.newaxis]\n",
    "im_pillow_c3 = im_pillow_c3[:, :, np.newaxis]"
   ],
   "id": "9f91b4f52e48cb2a",
   "outputs": [],
   "execution_count": 73
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T17:21:06.320023Z",
     "start_time": "2025-04-30T17:21:06.229503Z"
    }
   },
   "cell_type": "code",
   "source": [
    "zeros = np.zeros((im_array.shape[0], im_array.shape[1], 1)) #生成全0数组\n",
    "im_pillow_c1_3ch = np.concatenate((im_pillow_c2, zeros, zeros),axis=2)\n",
    "im_pillow_c1_3ch"
   ],
   "id": "faa384928d238b38",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[53.,  0.,  0.],\n",
       "        [56.,  0.,  0.],\n",
       "        [56.,  0.,  0.],\n",
       "        ...,\n",
       "        [62.,  0.,  0.],\n",
       "        [56.,  0.,  0.],\n",
       "        [49.,  0.,  0.]],\n",
       "\n",
       "       [[57.,  0.,  0.],\n",
       "        [59.,  0.,  0.],\n",
       "        [59.,  0.,  0.],\n",
       "        ...,\n",
       "        [61.,  0.,  0.],\n",
       "        [58.,  0.,  0.],\n",
       "        [53.,  0.,  0.]],\n",
       "\n",
       "       [[61.,  0.,  0.],\n",
       "        [63.,  0.,  0.],\n",
       "        [63.,  0.,  0.],\n",
       "        ...,\n",
       "        [60.,  0.,  0.],\n",
       "        [61.,  0.,  0.],\n",
       "        [58.,  0.,  0.]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[10.,  0.,  0.],\n",
       "        [10.,  0.,  0.],\n",
       "        [10.,  0.,  0.],\n",
       "        ...,\n",
       "        [13.,  0.,  0.],\n",
       "        [ 9.,  0.,  0.],\n",
       "        [ 8.,  0.,  0.]],\n",
       "\n",
       "       [[ 8.,  0.,  0.],\n",
       "        [ 9.,  0.,  0.],\n",
       "        [13.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 6.,  0.,  0.],\n",
       "        [12.,  0.,  0.],\n",
       "        [10.,  0.,  0.]],\n",
       "\n",
       "       [[ 8.,  0.,  0.],\n",
       "        [ 9.,  0.,  0.],\n",
       "        [13.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 6.,  0.,  0.],\n",
       "        [12.,  0.,  0.],\n",
       "        [10.,  0.,  0.]]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 89
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### OpenCV方式",
   "id": "8140bcce9b4a4c0c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T12:42:44.989292Z",
     "start_time": "2025-04-30T12:42:44.838253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import cv2\n",
    "im_cv2 = cv2.imread('files/test.jpg')\n",
    "type(im_cv2)"
   ],
   "id": "d63c03ae7723ca78",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-30T16:31:54.637205Z",
     "start_time": "2025-04-30T16:31:54.606660Z"
    }
   },
   "cell_type": "code",
   "source": "im_cv2.shape",
   "id": "55e3a845e9342dab",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2234, 3456, 3)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "***Pillow 读⼊后通道的顺序就是 R、G、B，⽽ OpenCV 读⼊后顺B、G、R***",
   "id": "4b5a9a5106558c4"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
